<template>
  <div class="execution">
    <basic-container>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="退款申请" name="0"></el-tab-pane>
        <el-tab-pane label="退款中" name="1"></el-tab-pane>
        <el-tab-pane label="已退款" name="2"></el-tab-pane>
        <el-tab-pane label="已驳回" name="3"></el-tab-pane>
      </el-tabs>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :search.sync="searchForm"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="rowDel"
        @search-reset="searchReset"
        @selection-change="selectionChange"
      >
        <!-- <template slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-notebook-2"
            v-if="activeName != 'second' || activeName == 'Four'"
            @click="pays"
          >
            合并缴费
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-tickets"
            v-if="activeName == 'second' || activeName == 'Four'"
            @click="invoice"
          >
            合并开票
          </el-button>
        </template> -->
        <template slot-scope="scope" slot="payCostSearch">
          <el-date-picker
            v-model="searchForm.payCost"
            type="month"
            format="yyyy-MM"
            value-format="yyyy-MM"
            placeholder="选择缴纳月份"
          >
          </el-date-picker>
        </template>
        <template slot-scope="scope" slot="totalAmount">
          {{
            scope.row.isOneData
              ? scope.row.totalAmount +
                "(含保证金:" +
                scope.row.guaranteeAmount +
                ")"
              : scope.row.totalAmount
          }}
        </template>
        <template slot-scope="scope" slot="monthSearch">
          <el-input-number
            v-model="searchForm.month"
            controls-position="right"
            :min="0"
          ></el-input-number>
        </template>
        <template slot="searchMenu">
          <el-button size="small" icon="el-icon-download" @click="exportExcel"
            >导出</el-button
          >
        </template>
        <template slot-scope="scope" slot="buildingTypeSearch">
          <el-select
            v-model="searchForm.buildingType"
            clearable
            ref="buildingType"
          >
            <el-option
              v-for="(item, index) in refundTypeOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="buildingType">
          {{ scope.row.buildingType | refundTypeFilter(that) }}
        </template>
        <template slot-scope="scope" slot="rentEndTime">
          {{ scope.row.rentStartTime + "至" + scope.row.rentEndTime }}
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            size="small"
            type="text"
            @click="detailView(scope.row)"
            v-if="scope.row.refundStatus == 0"
            >审核</el-button
          >
          <el-button
            size="small"
            type="text"
            @click="detailView(scope.row)"
            v-else
            >查看</el-button
          >
        </template>
      </avue-crud>
      <pay ref="pay" @refreshList="refreshList"></pay>
      <detail ref="detail"></detail>
      <payrecroed ref="payrecroed"></payrecroed>

      <detail-view ref="detailsForm" @checkStatus="checkStatus"></detail-view>
      <form-check ref="formCheck" @refreshList="refreshList"></form-check>
    </basic-container>
    <!-- 查看 -->
  </div>
</template>

<script>
import { fetchList } from "@/api/cost/refundCost";
import { tableOption } from "@/const/crud/cost/refundCost";
import { mapGetters } from "vuex";
import { getDicType, ExportFile } from "@/api/common";

import pay from "@/components/invoice/pay";
import formCheck from "./form";
import detail from "./detail";
import detailView from "./details";
import payrecroed from "./payrecroed";
export default {
  name: "cothercost",
  components: { pay, detail, payrecroed, detailView, formCheck },
  data() {
    return {
      that: this,
      searchForm: {
        isMonth: 0,
        isPayment: null,
        chargeType: 1,
        buildingType: "",
        payCost: "",
        month: 0,
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      activeName: "0",
      refundTypeOptions: [
        { label: "租赁保证金", value: 1 },
        { label: "装修保证金", value: 2 },
        { label: "会议租赁保证金", value: 3 },
        { label: "活动场地租赁保证金", value: 4 },
      ],
      selectList: [],
    };
  },
  filters: {
    refundTypeFilter(value, that) {
      let str = "";
      for (var i = 0; i < that.refundTypeOptions.length; i++) {
        if (that.refundTypeOptions[i].value == value) {
          str = that.refundTypeOptions[i].label;
          break;
        }
      }
      return str;
    },
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.generator_cothercost_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.generator_cothercost_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.generator_cothercost_edit,
          false
        ),
      };
    },
  },
  mounted() {},
  methods: {
    exportExcel() {
      // if (this.activeName == "0") {
      //   this.searchForm.isMonth = 0;
      //   this.searchForm.isPayment = 0;
      //   this.searchForm.arrearage = 0;
      // } else if (this.activeName == "1") {
      //   this.searchForm.isPayment = 1;
      //   this.searchForm.arrearage = 0;
      //   this.searchForm.isMonth = null;
      // } else if (this.activeName == "2") {
      //   this.searchForm.isPayment = 0;
      //   this.searchForm.isMonth = null;
      //   this.searchForm.arrearage = 0;
      // } else if (this.activeName == "3") {
      //   this.searchForm.isPayment = 0;
      //   this.searchForm.arrearage = 1;
      // }
      // this.searchForm.chargeType = 1;
      let props = {
        current: this.page.currentPage,
        size: this.page.pageSize,
        updateDate: this.searchForm.updateDate,
        orderNumber: this.searchForm.oldOrderId,
        companyName: this.searchForm.companyName,
        projectNumber: this.searchForm.projectNumber,
        refundStatus: this.activeName,
      };

      ExportFile("/cost/park/refundCost/export", props);
    },
    // 缴纳记录
    payrecroed(id) {
      this.$refs.payrecroed.init(id);
    },
    detail(id) {
      this.$refs.detail.init(id);
    },
    detailView(row) {
      this.$refs.detailsForm.init(row.id);
    },
    pay(item) {
      this.$refs.pay.init([item], 1);
    },
    refreshList() {
      this.getList();
    },
    checkStatus(status, id, gold) {
      console.log(status);
      this.$refs.formCheck.init(status, id, gold);
    },
    handleClick(tab, event) {
      this.tableOption.column[6].search = false;
      this.tableOption.column[6].hide = true;
      this.tableOption.column[7].hide = true;
      this.tableOption.column[5].search = false;
      this.tableOption.column[5].searchslot = false;
      if (this.activeName == "second") {
        this.tableOption.column[5].search = true;
        this.tableOption.column[5].searchslot = true;
      } else if (this.activeName == "forth") {
        this.tableOption.column[5].search = true;
        this.tableOption.column[5].searchslot = true;
        this.tableOption.column[6].hide = false;
        this.tableOption.column[7].hide = false;
      }
      this.getList();
    },
    getList(page, params) {
      this.tableLoading = true;
      if (this.activeName == "0") {
        this.searchForm.isMonth = 0;
        this.searchForm.isPayment = 0;
        this.searchForm.arrearage = 0;
      } else if (this.activeName == "1") {
        this.searchForm.isPayment = 1;
        this.searchForm.arrearage = 0;
        this.searchForm.isMonth = null;
      } else if (this.activeName == "2") {
        this.searchForm.isPayment = 0;
        this.searchForm.isMonth = null;
        this.searchForm.arrearage = 0;
      } else if (this.activeName == "3") {
        this.searchForm.isPayment = 0;
        this.searchForm.arrearage = 1;
      }
      this.searchForm.chargeType = 1;
      console.log(params);
      console.log(this.searchForm);

      fetchList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
            updateDate: this.searchForm.updateDate,
            orderNumber: this.searchForm.oldOrderId,
            companyName: this.searchForm.companyName,
            projectNumber: this.searchForm.projectNumber,
            refundStatus: this.activeName,
          }
          // params,
          // this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    rowDel: function (row, index) {
      this.$confirm("是否确认删除此条数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(row.id);
        })
        .then((data) => {
          this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
    handleUpdate: function (row, index, done, loading) {
      putObj(row)
        .then((data) => {
          this.$message.success("修改成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    handleSave: function (row, done, loading) {
      addObj(row)
        .then((data) => {
          this.$message.success("添加成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    searchChange(form, done) {
      // this.searchForm = form
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    refreshChange() {
      this.getList(this.page);
    },
    searchReset() {
      this.searchForm.updateDate = "";
      this.searchForm.oldOrderId = "";
      this.searchForm.companyName = "";
      this.searchForm.projectNumber = "";
      this.getList(this.page);
    },
    selectionChange(list) {
      this.selectList = list;
    },
    isIdentical(type) {
      let flag = true;
      let idList = [];
      this.selectList.some((item, index) => {
        if (idList.includes(item.companyId)) {
        } else {
          idList.push(item.companyId);
        }
        if (idList.length >= 2) {
          flag = false;
          return true;
        }
      });
      if (!flag) {
        let name = type ? "缴费" : "开票";
        let title = type ? "缴费提示" : "开票提示";
        this.$alert(
          "<div>当前选中的公司不一致，无法进行" + name + "操作</div>",
          title,
          {
            dangerouslyUseHTMLString: true,
          }
        );
      }
      return flag;
    },
    // pays () {
    //   if (this.selectList.length >= 2 && !this.isIdentical(1)) {
    //     return
    //   }
    //   let IsPay = true
    //   let indexList = []
    //   this.selectList.forEach((item, index) => {
    //     if (item.isPayment == 1) {
    //       IsPay = false
    //       indexList.push(item.$index + 1)
    //     }
    //   })
    //   if (IsPay) {
    //     this.$refs.pay.init(this.selectList, 1)
    //   } else {
    //     this.$alert('<div>以下数据序号存在已缴费</div><div>序号分别为<span style=\'font-size:18px;font-weight: bold;\'>[' + indexList + ']</span></div>', '缴费提示', {
    //       dangerouslyUseHTMLString: true
    //     });
    //   }
    // },

    // invoice () {
    //   if (this.selectList.length >= 2 && !this.isIdentical(0)) {
    //     return
    //   }
    //   let IsPay = true
    //   let indexList = []
    //   this.selectList.forEach((item, index) => {
    //     if (item.isPayment == 0 || item.isInvoice == 0) {
    //       IsPay = false
    //       indexList.push(item.$index + 1)
    //     }
    //   })
    //   if (IsPay) {
    //     this.$refs.pay.init(this.selectList, 1, null, 1)
    //   } else {
    //     this.$alert('<div>勾选数据中存在未缴费或已开票的数据</div><div>序号分别为<span style=\'font-size:18px;font-weight: bold;\'>[' + indexList + ']</span></div>', '开票提示', {
    //       dangerouslyUseHTMLString: true
    //     });
    //   }
    // }
  },
};
</script>
